package com.leo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;


/**
 *   页面跳转方法
 *
 *   create by leo
 *
*/
public class NavigationUtils {

    /**
     *
     *   Activity跳转
     * @param act
     * @param toActivity
     * @param bundle            传的参数
     * @param enterAnim         进入动画   R.anim...
     * @param exitAnim          出去动画   R.anim...
     * @param finish            是否关闭原有
     */
    public static final void startActivity(Activity act, Class<?> toActivity, Bundle bundle, int enterAnim, int exitAnim, boolean finish) {
        Intent intent = new Intent(act, toActivity);

        if (CheckUtils.isNull(bundle)) {
            bundle = new Bundle();
        }
        intent.putExtras(bundle);

        act.startActivity(intent);
        if (enterAnim > 0 && exitAnim > 0) {
            act.overridePendingTransition(enterAnim, exitAnim);
        }
        if (finish) {
            act.finish();
        }
    }

    public static final void startActivity(Activity act, Class<?> toActivity, Bundle bundle) {
        startActivity(act, toActivity, bundle, 0, 0, false);
    }

    public static final void startActivity(Activity act, Class<?> toActivity) {
        startActivity(act, toActivity, null, 0, 0, false);
    }

    public static void startActivity(Context context, Intent intent) {
        if (CheckUtils.isNotNull(intent) && CheckUtils.isNotNull(context)) {
            context.startActivity(intent);
        }
    }

    public static final void startActivityForResult(Activity act, Class<?> toActivity, Bundle bundle, int requestCode) {
        Intent intent = new Intent(act, toActivity);
        if (CheckUtils.isNull(bundle)) {
            bundle = new Bundle();
        }
        intent.putExtras(bundle);
        act.startActivityForResult(intent, requestCode);
    }

    public static final void finish(Activity act) {
        finish(act, -1, null);
    }

    /**
     *   activity 关闭
     * @param act
     * @param resultCode
     * @param intent
     */
    public static final void finish(Activity act, int resultCode, Intent intent) {
        finish(act,resultCode,intent,0,0);
    }

    public static final void finish(Activity act, int resultCode, Intent intent,int enterAnim, int exitAnim) {
        if (CheckUtils.isNull(intent)) {
            if (resultCode >= 0)
                act.setResult(resultCode);
        } else {
            act.setResult(resultCode, intent);
        }
        act.finish();

        if (enterAnim > 0 && exitAnim > 0) {
            act.overridePendingTransition(enterAnim, exitAnim);
        }

    }

    public static void setResult(Activity act, int resultCode, Intent intent) {
        if (CheckUtils.isNull(intent)) {
            act.setResult(resultCode);
        } else {
            act.setResult(resultCode, intent);
        }
    }
}
